const com = require('./com')
const cheerio = require('cheerio')
const iconv = require('iconv-lite')

let domain = 'http://wap.99166.com'

let url = 'http://wap.99166.com/sub_xm/bbqmdq/'

start()

async function start() {
    let res = await com.req(url)
    let body = iconv.decode(res.rawBody, 'gb2312').toString()
    , $ = cheerio.load(body)
    , chap = $('#qmdq1_box td>a')
    //console.log(chap.length)
    let all_data = []
    , all_data_obj = {}
    for(let i = 0; i < chap.length; i ++) {
        let row = $(chap[i])
        let href = row.attr('href')
        let title = row.text()
        let u = `${domain}${href}`
        com.elog(`## [${i+1}/${chap.length}]正在采集${title}  ${u}`)
        //console.log(title)
        let res2 = await com.req(u)
        let body2 = iconv.decode(res2.rawBody, 'gb2312').toString()
        let $2 = cheerio.load(body2)
        let chap2 = $2('#divInfor p')
        let name = title
        , introduce = com.trim($2('.text').find('p').eq(0).text())
        , offer = []
        , boy = {
            title: '',
            text: [],
            wuXing: {
                jin: {title: '', name: []},
                mu: {title: '', name: []},
                shui: {title: '', name: []},
                huo: {title: '', name: []},
                tu: {title: '', name: []},
            },
        }
        , girl = {
            title: '',
            text: [],
            wuXing: {
                jin: {title: '', name: []},
                mu: {title: '', name: []},
                shui: {title: '', name: []},
                huo: {title: '', name: []},
                tu: {title: '', name: []},
            },
        }
        introduce = introduce.replace('导语：', '')
        /* let data = {
            name: title,
            introduce: ''
        } */
        // console.log(chap2.length)
        let flag = 0
        for(let j = 0; j < chap2.length; j ++) {
            let row2 = $(chap2[j])
            if(row2.attr('align') == 'center') {
                continue
            }
            let tt = com.trim(row2.text())
            if(tt.includes('八字五行起名法建议')) {
                flag = 1
                continue
            } else if(introduce.length == 0 && tt.includes('起源')) {
                flag = 1
                introduce = tt
                continue
            } else if(tt.includes('男孩起名')) {
                boy.title = tt
                flag = 2
                continue
            } else if(flag >=2 && flag < 8 && tt.includes('最喜五行为“金”')) {
                boy.wuXing.jin.title = tt
                flag = 3
                continue
            } else if(flag >=2 && flag < 8 && tt.includes('最喜五行为“木”')) {
                boy.wuXing.mu.title = tt
                flag = 4
                continue
            } else if(flag >=2 && flag < 8 && tt.includes('最喜五行为“水”')) {
                boy.wuXing.shui.title = tt
                flag = 5
                continue
            } else if(flag >=2 && flag < 8 && tt.includes('最喜五行为“火”')) {
                boy.wuXing.huo.title = tt
                flag = 6
                continue
            } else if(flag >=2 && flag < 8 && tt.includes('最喜五行为“土”')) {
                boy.wuXing.tu.title = tt
                flag = 7
                continue
            } else if(tt.includes('女孩起名')) {
                girl.title = tt
                flag = 8
                continue
            }  else if(flag >= 8 && tt.includes('最喜五行为“金”')) {
                girl.wuXing.jin.title = tt
                flag = 9
                continue
            } else if(flag >= 8 && tt.includes('最喜五行为“木”')) {
                girl.wuXing.mu.title = tt
                flag = 10
                continue
            } else if(flag >= 8 && tt.includes('最喜五行为“水”')) {
                girl.wuXing.shui.title = tt
                flag = 11
                continue
            } else if(flag >= 8 && tt.includes('最喜五行为“火”')) {
                girl.wuXing.huo.title = tt
                flag = 12
                continue
            } else if(flag >= 8 && tt.includes('最喜五行为“土”')) {
                girl.wuXing.tu.title = tt
                flag = 13
                continue
            }


            if(flag == 1) {
                offer.push(tt)
            } else if(flag == 2) {
                boy.text.push(tt)
            } else if(flag == 3) {
                boy.wuXing.jin.name.push(tt)
            } else if(flag == 4) {
                boy.wuXing.mu.name.push(tt)
            } else if(flag == 5) {
                boy.wuXing.shui.name.push(tt)
            } else if(flag == 6) {
                boy.wuXing.huo.name.push(tt)
            } else if(flag == 7) {
                boy.wuXing.tu.name.push(tt)
            } else if(flag == 8) {
                girl.text.push(tt)
            } else if(flag == 9) {
                girl.wuXing.jin.name.push(tt)
            } else if(flag == 10) {
                girl.wuXing.mu.name.push(tt)
            } else if(flag == 11) {
                girl.wuXing.shui.name.push(tt)
            } else if(flag == 12) {
                girl.wuXing.huo.name.push(tt)
            } else if(flag == 13) {
                girl.wuXing.tu.name.push(tt)                
            }
        }
        let data = {
            name: name,
            introduce: introduce,
            offer: offer,
            sex: [boy, girl]
        }
        com.wFile(`./data/${name}.json`, JSON.stringify(data))
        all_data_obj[name] = data
        com.elog(`## [${i+1}/${chap.length}] ${name} 采集完毕`)
        //break
    }
    com.wFile(`./data/_取名大全.json`, JSON.stringify(all_data_obj))
    com.elog(`## 全部采集完毕`)

}